package com.example.home_stretch;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.http.GET;

public class Traffic_light_management extends AppCompatActivity {
    @BindView(R.id.sp_px)
    Spinner spPx;
    @BindView(R.id.sp_lv)
    ListView spLv;
    private List<Traffic_lights_list> hld_list = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.traffic);
        ButterKnife.bind(this);
        initdata();
        initview();

    }

    public interface BlogService {
        @GET("getTrafficLightRGY")
        Call<ResponseBody> getBlog();
    }
    //spinner的使用
    private void initdata(){
        final String[] sspinner=getResources().getStringArray(R.array.traffic);
        ArrayAdapter<String>adapter=new ArrayAdapter<String>(Traffic_light_management.this,R.layout.support_simple_spinner_dropdown_item,sspinner);
        spPx.setAdapter(adapter);
    }
    private void initview() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http:hh1.me:5001/")
                .build();
        BlogService service = retrofit.create(BlogService.class);
        Call<ResponseBody> call = service.getBlog();
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                Gson gson = new Gson();
                try {
                    String s = response.body().string();
                    Log.i("TAG", "onResponsadadad"+s);
                    Traffic_lights_list carlist = gson.fromJson(s, Traffic_lights_list.class);
                    hld_list.add(carlist);
                    spLv.setAdapter(new MyAdapter());
                    spPx.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                        @Override
                        public void onItemSelected(AdapterView<?> parent, View view, int i, long id) {
                            switch (i) {
                                case 0:
                                    Collections.sort(hld_list.get(0).getData(),new sort1());
                                    Log.i("TAG", "onItemSelected" + 123);
                                    spLv.setAdapter(new MyAdapter());
                                    break;
                                case 1:
                                    Collections.sort(hld_list.get(0).getData(),new sort2());
                                    Log.i("TAG", "onItemSelected" + 123);
                                    spLv.setAdapter(new MyAdapter());
                                    break;
                                case 2:
                                    Collections.sort(hld_list.get(0).getData(),new sort3());
                                    Log.i("TAG", "onItemSelected" + 123);
                                    spLv.setAdapter(new MyAdapter());
                                    break;
                                case 3:
                                    Collections.sort(hld_list.get(0).getData(),new sort4());
                                    Log.i("TAG", "onItemSelected" + 123);
                                    spLv.setAdapter(new MyAdapter());
                                    break;
                            }
                        }
                        @Override
                        public void onNothingSelected(AdapterView<?> parent) {
                            Log.i("TAG", "onNothingSelected"+"验证消息");
                        }
                    });
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
            }
        });
    }

    class MyAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return hld_list.get(0).getData().size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }
        class ViewHolder {
            @BindView(R.id.tv_lk)
            TextView tvLk;
            @BindView(R.id.tv_red)
            TextView tvRed;
            @BindView(R.id.tv_yellow)
            TextView tvYellow;
            @BindView(R.id.tv_green)
            TextView tvGreen;
            ViewHolder(View view) {
                ButterKnife.bind(this, view);
            }
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view1 = View.inflate(Traffic_light_management.this, R.layout.traffic_one, null);
            ViewHolder viewHolder = new ViewHolder(view1);
            viewHolder.tvLk.setText(hld_list.get(0).getData().get(position).getRoad()+"");
            viewHolder.tvRed.setText(hld_list.get(0).getData().get(position).getRed()+"");
            viewHolder.tvYellow.setText(hld_list.get(0).getData().get(position).getYellow()+"");
            viewHolder.tvGreen.setText(hld_list.get(0).getData().get(position).getGreen()+"");
            return view1;
        }
    }
    class sort1 implements  Comparator<Traffic_lights_list.DataBean>{
        @Override
        public int compare(Traffic_lights_list.DataBean o1, Traffic_lights_list.DataBean o2) {
            return o1.getRoad()-o2.getRoad();
        }
    }
    class sort3 implements Comparator<Traffic_lights_list.DataBean>{
        @Override
        public int compare(Traffic_lights_list.DataBean o1, Traffic_lights_list.DataBean o2) {
            return Integer.parseInt(o1.getGreen())-Integer.parseInt(o2.getGreen());
        }
    }
    class sort2 implements Comparator<Traffic_lights_list.DataBean>{
        @Override
        public int compare(Traffic_lights_list.DataBean o1, Traffic_lights_list.DataBean o2) {
            return Integer.parseInt(o1.getRed())-Integer.parseInt(o2.getRed());
        }
    }
    class sort4 implements Comparator<Traffic_lights_list.DataBean>{
        @Override
        public int compare(Traffic_lights_list.DataBean o1, Traffic_lights_list.DataBean o2) {
            return Integer.parseInt(o1.getYellow())-Integer.parseInt(o2.getYellow());
        }
    }
}
